export function parseXMLToAdjacencyList(xmlStr) {
    // 使用DOMParser解析XML字符串
    function parseXml(xmlStr) {
        const parser = new DOMParser();
        return parser.parseFromString(xmlStr, "text/xml");
    }

    // 提取所有<mxCell>元素的信息
    function extractCells(xmlDoc) {
        const cells = [];
        const mxCells = xmlDoc.getElementsByTagName("mxCell");

        for (let i = 0; i < mxCells.length; i++) {
            const cell = mxCells[i];
            const id = cell.getAttribute("id");
            const style = cell.getAttribute("style");
            const vertex = cell.getAttribute("vertex");
            const edge = cell.getAttribute("edge");
            const source = cell.getAttribute("source");
            const target = cell.getAttribute("target");
            const titles = cell.getAttribute("titles"); // 获取titles属性

            if (id && (vertex || edge)) {
                cells.push({
                    id: id,
                    style: style,
                    vertex: vertex === "1",
                    edge: edge === "1",
                    source: source,
                    target: target,
                    titles: titles, // 添加titles属性
                    geometry: cell.getElementsByTagName("mxGeometry").length > 0 ? {
                        x: cell.getElementsByTagName("mxGeometry")[0].getAttribute("x"),
                        y: cell.getElementsByTagName("mxGeometry")[0].getAttribute("y"),
                        width: cell.getElementsByTagName("mxGeometry")[0].getAttribute("width"),
                        height: cell.getElementsByTagName("mxGeometry")[0].getAttribute("height")
                    } : null
                });
            }
        }

        return cells;
    }

    // 根据提取的单元格信息构建邻接表并找到开始节点ID
    function buildAdjacencyList(cells) {
        const adjacencyList = {};
        let startNodeId = null;

        cells.forEach(cell => {
            if (cell.edge && cell.source && cell.target) {
                if (!adjacencyList[cell.source]) adjacencyList[cell.source] = [];
                adjacencyList[cell.source].push(cell.target);

                // 如果是无向图，则也需要将目标节点作为源节点添加
                if (!adjacencyList[cell.target]) adjacencyList[cell.target] = [];
                adjacencyList[cell.target].push(cell.source);
            }
            if (cell.titles === '开始') {
                startNodeId = cell.id;
            }
        });

        return { adjacencyList, startNodeId };
    }

    // 主流程
    try {
        const xmlDoc = parseXml(xmlStr);
        const cells = extractCells(xmlDoc);
        const { adjacencyList, startNodeId } = buildAdjacencyList(cells);

        return { adjacencyList, startNodeId };
    } catch (error) {
        console.error('Error parsing XML:', error);
        return {};
    }
}

// 深度优先搜索(DFS)算法来检查从起点到终点的路径
export function isConnected(adjList, startNode, targetNode, visited = new Set()) {
    console.log(startNode,targetNode,"targetNode")
    if (startNode === targetNode) return true; // 找到了目标节点
    visited.add(startNode);

    const neighbors = adjList[startNode] || [];
    for (let neighbor of neighbors) {
        if (!visited.has(neighbor)) {
            if (isConnected(adjList, neighbor, targetNode, visited)) return true;
        }
    }

    return false;
}


// 示例调用
// const xmlString = `...`; // 你的XML字符串
// const adjacencyList = parseXMLToAdjacencyList(xmlString);



// 假设我们想检查 'rectangleFull-11' 是否与 'startCircle-1' 有直接或间接的连线
// const selectedNodeId = 'rectangleFull-11';
// const startNodeId = 'startCircle-1';

// if (isConnected(adjacencyList, startNodeId, selectedNodeId)) {
//     console.log(`${selectedNodeId} 与 ${startNodeId} 有直接或间接的连线`);
// } else {
//     console.log(`${selectedNodeId} 与 ${startNodeId} 没有直接或间接的连线`);
// }